package com.sdy.resdir.biz.service.impl;

import com.sdy.auth.api.model.UserInfo;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.resdir.biz.mapper.RdResCatalogTableInfoMapper;
import com.sdy.resdir.biz.model.RdResCatalogTableInfo;
import com.sdy.resdir.biz.model.RdResourceCataloging;
import com.sdy.resdir.biz.mapper.RdResourceCatalogingMapper;
import com.sdy.resdir.biz.service.RdResourceCatalogingService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源编目 服务实现类
 * </p>
 *
 * @author hyh
 * @since 2019-10-21
 */
@Slf4j
@Service
public class RdResourceCatalogingServiceImpl extends BaseServiceImpl<RdResourceCataloging> implements RdResourceCatalogingService {
    @Resource
    private RdResourceCatalogingMapper resCatalogingMapper;
    @Resource
    private RdResCatalogTableInfoMapper tableInfoMapper;

    @Override
    public int saveOrUpdateResLog(RdResourceCataloging cataloging, UserInfo user) throws BizException {
        int resState;
        int tableInfoUpdateState = 0;
        int tableInfoAddState = 0;
        if (null != cataloging.getId()) {
            // 修改资源编目信息时不能够修改绑定的资源id
            cataloging.setOwnResId(null);
            // 更新资源编目信息
            resState = resCatalogingMapper.updateById(cataloging);
            List<RdResCatalogTableInfo> addTable = cataloging.getTableInfos().stream().filter(table -> table.getId() == null).collect(Collectors.toList());
            List<RdResCatalogTableInfo> updateTable = cataloging.getTableInfos().stream().filter(table -> table.getId() != null).collect(Collectors.toList());
            Date now = new Date();
            for (RdResCatalogTableInfo info : addTable) {
                info.setResCatalogId(cataloging.getId());
                info.setCreateTime(now);
            }
            if (!updateTable.isEmpty()) {
                tableInfoUpdateState = tableInfoMapper.batchUpdate(updateTable);
            }
            if (!addTable.isEmpty()) {
                tableInfoAddState = tableInfoMapper.batchInsert(addTable);
            }
            if (addTable.isEmpty()) {
                tableInfoAddState = 1;
            }
            Assert.isTrue(resState == 0 || tableInfoUpdateState == 0 || tableInfoAddState == 0, "操作出现异常,请重试");
        } else {
            // 新增资源编目信息
            Assert.isNull(cataloging.getOwnResId(), "新增资源编目必须绑定资源！");
            cataloging.setCatalogingTime(new Date());
            resState = resCatalogingMapper.insert(cataloging);
            List<RdResCatalogTableInfo> addTable = cataloging.getTableInfos().stream().filter(table -> table.getId() == null).collect(Collectors.toList());
            List<RdResCatalogTableInfo> updateTable = cataloging.getTableInfos().stream().filter(table -> table.getId() != null).collect(Collectors.toList());
            Date now = new Date();
            for (RdResCatalogTableInfo info : addTable) {
                info.setResCatalogId(cataloging.getId());
                info.setCreateTime(now);
            }
            if (!updateTable.isEmpty()) {
                tableInfoUpdateState = tableInfoMapper.batchUpdate(updateTable);
            }
            if (!addTable.isEmpty()) {
                tableInfoAddState = tableInfoMapper.batchInsert(addTable);
            }
            if (updateTable.isEmpty()) {
                tableInfoUpdateState = 1;
            }
            Assert.isTrue(resState == 0 || tableInfoUpdateState == 0 || tableInfoAddState == 0, "操作出现异常,请重试");
        }
        return 1;
    }
}
